//
//  VideoProgress.m
//  HappyBaby
//
//  Created by 华哥 on 15/11/24.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "VideoProgress.h"
#import "Define.h"

@interface VideoProgress ()
@property (nonatomic, strong) UIView *cacheView;
@end

@implementation VideoProgress
{
    CGFloat centerX; // 保存center位置;
}

- (void)dealloc
{
    _delegate = nil;
    [self.thumb removeObserver:self forKeyPath:@"frame"];
    [self.slider removeObserver:self forKeyPath:@"value"];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        float scaleX = kScreenWidth / 375;
        float scaleY = kScreenHeight / 667;
        
        // centerX 初始值
        centerX = 0;
        
        // 添加kvo, 用户改变了frame得大小, 我们对center做一个修改
        [self.thumb addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
        
        //        slider
        self.slider = [[UISlider alloc]initWithFrame:self.bounds];
        self.slider.value = 0;
        [_slider setThumbImage:[UIImage imageNamed:@"nil"] forState:UIControlStateNormal];
        
        [_slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
        
        // 添加tap手势, 让滑块跳转到指定位置
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tap];
        [self addSubview:_slider];
        
        //        我们的缓冲条在我们的slider上面, 滑块下面
        self.cacheView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 2 * scaleY)];
        // 用户交互关闭
        self.cacheView.userInteractionEnabled = NO;
        [self addSubview:_cacheView];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30 * scaleX, 15 * scaleY)];
        imageView.center = CGPointMake(0, self.frame.size.height / 2 );
        imageView.image = [UIImage imageNamed:@"bird"];
        self.thumb = imageView;
//        _thumb.center = CGPointMake(, frame.size.height / 3);
//        _thumb.backgroundColor = [UIColor redColor];
        _thumb.userInteractionEnabled = NO;
        self.thumb.layer.cornerRadius = 10;
        [self addSubview:_thumb];
        
        // 添加一个KVO观察我们slider的value值, 然后对滑块的位置作修改;
        [self.slider addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

#pragma mark --- tap手势执行方法 ---
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    // 我们要取出tap手势上面的一个点
    CGPoint point = [tap locationInView:self];
    NSLog(@"%f", point.x);
    // 范围判断
    if (point.x < 0)
    {
        point.x = 0;
    }
    else if (point.x > self.frame.size.width)
    {
        point.x = self.frame.size.width;
    }
    // 求出这个点所在的百分比
    CGFloat progress = point.x / self.frame.size.width;
    // 根据百分比计算value的值
    CGFloat value = (self.slider.maximumValue - self.slider.minimumValue) * progress;
    self.slider.value = value;
    self.thumb.center = CGPointMake(point.x, self.frame.size.height / 2);
    
    // 传递参数 (让代理执行的方法);
    if ([_delegate respondsToSelector:@selector(touchView:)])
    {
        [_delegate touchView:value];
    }
}

#pragma mark --- 滑动滑块执行方法 ---
- (void)sliderAction:(UISlider *)slider
{
    CGFloat progress = slider.value/(slider.maximumValue - slider.minimumValue);
    CGFloat thumbX = self.frame.size.width * progress;
    self.thumb.center = CGPointMake(thumbX, self.frame.size.height / 2);    
    centerX = self.thumb.center.x;
}

#pragma mark --- kvo执行方法 ---
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"frame"])
    {
        self.thumb.center = CGPointMake(centerX, self.frame.size.height / 2);
    }
    else
    {
        // 修改滑块的位置
        CGFloat progress = self.slider.value / (self.slider.maximumValue - self.slider.minimumValue);
        self.thumb.center = CGPointMake(self.frame.size.width * progress, self.frame.size.height / 2);
    }    
}

#pragma mark --- 更换视图执行方法 ---
- (void)setThumb:(UIView *)thumb
{
    // 先移除我们的观察者
    [_thumb removeObserver:self forKeyPath:@"frame"];
    // 移除之前的视图
    [_thumb removeFromSuperview];
    // 添加新的视图
    [self addSubview:thumb];
    // 指针的赋值;
    _thumb = thumb;
    // 添加新的观察者
    [_thumb addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
//     让他走一次KVO方法, 对center做改变;
    _thumb = thumb;
}

#pragma mark --- 外部改变frame, 内部子视图也作出相应的修改 ---
- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    self.slider.frame = self.bounds;
    self.thumb.center = CGPointMake(centerX, self.frame.size.height / 2);
}

#pragma mark --- 根据cacheValue进行赋值
- (void)setCache:(CGFloat)cache
{
    // 范围判断
    if (cache < self.slider.minimumValue)
    {
        cache = self.slider.minimumValue;
    }
    else if (cache > self.slider.maximumValue)
    {
        cache = self.slider.maximumValue;
    }
    // 求出百分比
    CGFloat progress = cache / (self.slider.maximumValue - self.slider.minimumValue);
    // 对cacheView进行赋值
    self.cacheView.frame = CGRectMake(0, (self.frame.size.height - 2) / 2, progress * self.frame.size.width, 2);
}

#pragma mark --- 更改我们缓冲条的背景颜色 ---
- (void)setCacheColor:(UIColor *)cacheColor
{
    _cacheColor = cacheColor;
    self.cacheView.backgroundColor = cacheColor;
}



@end
